package com.atguigu.gmallsuger.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmallsuger.bean.*;
import com.atguigu.gmallsuger.service.KWService;
import com.atguigu.gmallsuger.service.ProductService;
import com.atguigu.gmallsuger.service.ProvinceService;
import com.atguigu.gmallsuger.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/14 9:56
 */
@RestController
public class SugerController {
    
    @Autowired
    ProductService product;
    
    @RequestMapping("/suger/api/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        BigDecimal gmv = product.gmv(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        result.put("data", gmv);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/suger/api/gmv/tm")
    public String gmvByTM(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<Map<String, Object>> list = product.getGMVByTmName(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Map<String, Object> map : list) {
            String tm_name = (String) map.get("tm_name");
            categories.add(tm_name);
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        // TODO
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        JSONArray d = new JSONArray();
        
        for (Map<String, Object> map : list) {
            BigDecimal order_amount = (BigDecimal) map.get("order_amount");
            d.add(order_amount);
        }
        
        obj.put("data", d);
        
        series.add(obj);
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/suger/api/gmv/c3")
    public String gmvByC3(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<C3Stats> list = product.getGMVByC3Name(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (C3Stats c3Stats : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3Stats.getCategory3_name());
            obj.put("value", c3Stats.getOrder_amount());
            
            data.add(obj);
            
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/suger/api/gmv/spu")
    public String gmvBySpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<SpuStats> list = product.getGMVBySpuName(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu_name");
        c1.put("id", "spu");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (SpuStats spuStats : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("spu", spuStats.getSpu_name());
            obj.put("order_amount", spuStats.getOrder_amount());
            
            rows.add(obj);
            
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    ProvinceService province;
    
    @RequestMapping("/suger/api/map/province")
    public String province(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<ProvinceStats> list = this.province.province(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray mapData = new JSONArray();
        
        for (ProvinceStats ps : list) {
            // 一个省份一个对象
            JSONObject obj = new JSONObject();
            obj.put("name", ps.getProvince_name());
            obj.put("value", ps.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(ps.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
            
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    VisitorService visitor;
    
    @RequestMapping("/suger/api/visitor/hour")
    public String visitorByHour(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<VisitorStats> list = visitor.statsByHour(date);
        // hour -> VisitorStats
        HashMap<Integer, VisitorStats> hourToVs = new HashMap<>();
        for (VisitorStats vs : list) {
            hourToVs.put(vs.getHour(), vs);
        }
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        // 1 pv
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray data1 = new JSONArray();
        pv.put("data", data1);
        series.add(pv);
        // 2 uv
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray data2 = new JSONArray();
        uv.put("data", data2);
        series.add(uv);
        
        // 3 uj
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray data3 = new JSONArray();
        uj.put("data", data3);
        series.add(uj);
        
        for (int i = 0; i < 24; i++) {
            // 如何找到i点pv
            VisitorStats vs = hourToVs.getOrDefault(i, new VisitorStats());
            data1.add(vs.getPv());
            data2.add(vs.getUv());
            data3.add(vs.getUj());
        }
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/suger/api/visitor/vc")
    public String visitorByVc(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<VisitorStats> list = visitor.statsByVc(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "版本");
        c1.put("id", "vc");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "pv");
        c2.put("id", "pv");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "uv");
        c3.put("id", "uv");
        columns.add(c3);
        
        JSONObject c4 = new JSONObject();
        c4.put("name", "uj");
        c4.put("id", "uj");
        columns.add(c4);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        
        rows.addAll(list);
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    KWService kw;
    
    @RequestMapping("/suger/api/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = defaultDate(date);
        
        List<KwStats> list = kw.statsKeyword(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
    
        for (KwStats kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    private int defaultDate(int date) {
        // 如果date的值是0 表示没有传日期过来. 如果传递过来了,则date是有值
        if (date == 0) {
            date = Integer.parseInt(new SimpleDateFormat("YYYYMMdd").format(System.currentTimeMillis()));
        }
        return date;
    }
}
